package com.chapter.sample.juc.jmm;

import java.util.concurrent.TimeUnit;

/**
 * volatile 不保证原子性
 */
public class VolatileNoAtomicDemo {
    public static void main(String[] args) throws InterruptedException {
        MyNumber myNumber = new MyNumber();
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                for (int j = 0; j < 1000; j++) {
                    myNumber.addPlusPlus();
                }
            }, "thread:" + i).start();
        }

        TimeUnit.SECONDS.sleep(1);
        System.out.println(Thread.currentThread().getName() + " : " + myNumber.number);
    }
}

class MyNumber {
    volatile int number = 0;

    public void addPlusPlus() {
        number++;
    }
}